﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Globalization;
using MobileWebsiteGenerator.Utility;
using MobileWebsiteGenerator.Entities;
using System.Data;
using Mvc.Mailer;
using MobileWebsiteGenerator.Mailers;
using MobileWebsiteGenerator.Models;

namespace MobileWebsiteGenerator.Controllers
{
    public class HomeController : BaseController
    {
        private IUserMailer _userMailer = new UserMailer();
        public IUserMailer UserMailer
        {
            get { return _userMailer; }
            set { _userMailer = value; }
        }
        public Mobile_appContext context = new Mobile_appContext();
        public ActionResult Index()
        {
            ViewBag.Message = "";

            //var indexpage = context.WebsitePages.Where(p => p.ID == 1).FirstOrDefault();

            return View();

        }


        public ActionResult AdminMessages()
        {

            return View();
        }

        public ActionResult About()
        {
            //var aboutpage = context.WebsitePages.Where(p => p.ID == 2).FirstOrDefault();

            return View();
        }
        public ActionResult ContactUs()
        {

            return View();
        }

        public ActionResult test()
        {

            return View();
        }


        [HttpPost]
        public ActionResult ContactUs(ContactUs contactus)
        {
            UserMailer.ContactUs(contactus).Send();
            MvcApplication.showMessage = true;
            MvcApplication.messageTxt = "Thanks For Contacting us.";

            return View();
        }

        public ActionResult TakeAtour()
        {
            return View();
        }
        public ActionResult MailSent()
        {
            return View();
        }
        public ActionResult Feature()
        {
            return View();
        }
        public ActionResult Pricing()
        {
            //var pricingpage = context.WebsitePages.Where(p => p.ID == 4).FirstOrDefault();

            return View();
        }
        public ActionResult Showcase()
        {
            return View();
        }
        public ActionResult SignUp()
        {
            return View();
        }



        public ActionResult slides()
        {
            return View();
        }

        public ActionResult LastStep()
        {
            MvcApplication.IscreateNewFront = false;
            return View();
        }
        public ActionResult Login()
        {


            return View(MvcApplication._MainCustomer.customer);
        }
        [HttpPost]
        public ActionResult Login(Customer customer)
        {

            //Add all detail 



            var cust = context.Customers.Where(c => c.Email == customer.Email && c.Password == customer.Password).FirstOrDefault();
            if (cust != null)
            {
                if (cust.IsActive == true)
                {
                    MvcApplication.IsUpdateMode = true;
                    MvcApplication.IsLoggedIn = true;
                    MobileWebsiteGenerator.MvcApplication._MainCustomer.customer = cust;
                    var website = context.Websites.Where(web => web.web_id == context.CustomerWebsiteRels.Where(w => w.Customer_id == MvcApplication._MainCustomer.customer.Customer_id).Select(p => p.Web_id).FirstOrDefault()).FirstOrDefault();
                    MvcApplication._MainCustomer.Website = website;
                    if (MvcApplication._MainCustomer.Website != null)
                    {
                        var ThemeID = context.WebthemeRels.Where(w => w.Web_id == MvcApplication._MainCustomer.Website.web_id).Select(t => t.Theme_id).FirstOrDefault();
                        MvcApplication._MainCustomer.MyTheme = context.ThemeDetails.Where(t => t.theme_id == ThemeID).FirstOrDefault();
                        MvcApplication._MainCustomer.ContentPages = context.Pages.Where(p => context.ThemePageRELs.Where(c => c.Theme_id == MvcApplication._MainCustomer.MyTheme.theme_id).Select(x => x.Page_id).Contains(p.page_id)).OrderBy(p=>p.Priority).ToList();
                    
                      ViewBag.pagecount = MvcApplication._MainCustomer.ContentPages.Count();
                        if (website.IsContactusPage == true && MobileWebsiteGenerator.MvcApplication._MainCustomer.ContentPages.Count() != 0)
                            MvcApplication.pageId = MobileWebsiteGenerator.MvcApplication._MainCustomer.ContentPages.Where(p => p.title == "ContactUs").FirstOrDefault().page_id;
                        MvcApplication._MainCustomer.PictureGallery = context.pictureGalleries.Where(p => context.ThemePictureRELs.Where(c => c.Theme_id == MvcApplication._MainCustomer.MyTheme.theme_id).Select(x => x.Picture_id).Contains(p.picture_id)).ToList();
                        return RedirectToAction("UserHome", "Customer");
                    }
                    else
                    {
                        MvcApplication.IscreateNewFront = true;
                        return RedirectToAction("index", "themedetail");
                    }
                    return RedirectToAction("Create", "Website");
                }

                else
                {
                    MvcApplication.showMessage = true;
                    MvcApplication.messageTxt = "You are not activated yet";
                    return RedirectToAction("index", "Home");
                }
            }
            else
            {
                MvcApplication.showMessage = true;
                MvcApplication.messageTxt = "Email does not exist.";
                return RedirectToAction("index", "Home");
            }



            return View();
        }
        public ActionResult ForgotPassword()
        {
            return View();
        }

        [HttpPost]
        public ActionResult ForgotPassword(Customer customer)
        {
            var senddetail = context.Customers.Where(c => c.Email == customer.Email).FirstOrDefault();
            if (senddetail != null)
            {
                _userMailer.ForgotPass(senddetail).Send();
                MvcApplication.showMessage = true;
                MvcApplication.messageTxt = "Your Password is Send to your Email.";
                return RedirectToAction("index", "Home");
            }


            return View("_EmailNotExist");
        }
        public ActionResult Logout(Customer customer)
        {
            MvcApplication.IsLoggedIn = false;
            MvcApplication._MainCustomer = new Models.MainViewModel();
            return RedirectPermanent("Index");
        }


        public ActionResult SetCulture(string culture)
        {
            // Validate input
            culture = CultureHelper.GetValidCulture(culture);

            // Save culture in a cookie
            HttpCookie cookie = Request.Cookies["_culture"];
            if (cookie != null)
                cookie.Value = culture;   // update cookie value
            else
            {

                cookie = new HttpCookie("_culture");
                cookie.HttpOnly = false; // Not accessible by JS.
                cookie.Value = culture;
                cookie.Expires = DateTime.Now.AddYears(1);
            }
            Response.Cookies.Add(cookie);

            return RedirectToAction("Index");
        }
    }
}
